using System;
using System.Collections.Generic;
using System.Text;
using Impact.Model.Service;
using Impact.Model.Objects;

namespace Impact.UnitTests.Mocks
{
    public class MockArticleService : IArticleService
    {

        public MockArticleService()
        {
            articles = new List<Article>();
            Guid author = new Guid( "36BF4EB7-B677-4591-BD11-23423ABC36CB" );

            Article one = new Article();
            one.ArticleId = 1;
            one.CategoryId = 1;
            one.Title = "one";
            one.Body = "one";
            one.Enabled = true;
            one.CreatedBy = author.ToString();
            one.CreatedOn = new DateTime( 2007, 1, 1, 1, 1, 1, 0, DateTimeKind.Local );
            articles.Add( one );

            ArticleCommentCollection oneComments = FetchComments(1);

            ArticleComment comment1 = new ArticleComment();
            comment1.CommentId = 1;
            comment1.ArticleId = 1;
            comment1.Author = "tester";
            comment1.Body = "test comment one";
            oneComments.Add(comment1);

            ArticleComment comment2 = new ArticleComment();
            comment2.CommentId = 2;
            comment2.ArticleId = 1;
            comment2.Author = "tester";
            comment2.Body = "test comment two";
            oneComments.Add(comment2);

            Article two = new Article();
            two.ArticleId = 2;
            two.CategoryId = 2;
            two.Title = "two";
            two.Body = "two";
            two.Enabled = true;
            two.CreatedBy = author.ToString();
            two.CreatedOn = new DateTime( 2007, 2, 2, 2, 2, 2, 0, DateTimeKind.Local );
            articles.Add( two );

            Article three = new Article();
            three.ArticleId = 3;
            three.CategoryId = 3;
            three.Title = "three";
            three.Body = "three";
            three.Enabled = false;
            three.CreatedBy = author.ToString();
            three.CreatedOn = new DateTime( 2007, 3, 3, 3, 3, 3, 0, DateTimeKind.Local );
            articles.Add(three);

        }

        private List<Article> articles;
        public List<Article> Articles
        {
            get { return articles; }
        }

	

        #region IArticleService Members

        public Article FetchByID(int articleId)
        {
            return articles.Find(delegate(Article a)
            {
                return a.ArticleId == articleId;
            });
        }

        public ArticleCollection FetchByTerms(string terms)
        {
            ArticleCollection results = new ArticleCollection();
            results.AddRange(
                articles.FindAll(delegate(Article article)
                {
                    return article.Body.Contains(terms) || article.Title.Contains(terms);
                })
            );
            return results;
        }

        public ArticleCollection FetchByCategory(int categoryId, bool onlyEnabled)
        {
            ArticleCollection output = new ArticleCollection();
            output.AddRange( 
                articles.FindAll( delegate( Article article )
                {
                    if ( onlyEnabled )
                        return article.CategoryId == categoryId && article.Enabled == true;
                    else
                        return article.CategoryId == categoryId;
                } ) );
            return output;
        }

        public ArticleCollection FetchByCategory(string categoryKey, bool onlyEnabled)
        {
            throw new Exception("The method or operation is not implemented.");
        }

        public ArticleCommentCollection FetchComments(int ArticleId)
        {
            if (articleComments == null)
            {
                articleComments = new Dictionary<int, ArticleCommentCollection>();
            }
            if (!articleComments.ContainsKey(ArticleId))
            {
                ArticleCommentCollection comments = new ArticleCommentCollection();
                articleComments.Add(ArticleId, comments);
            }
            return articleComments[ArticleId];
        }
        private Dictionary<int, ArticleCommentCollection> articleComments;


        public ArticleTagCollection FetchTags(int articleId)
        {
            throw new Exception("The method or operation is not implemented.");
        }

        public void Save(Article article)
        {
            // do nothing
        }

        public void Save(Article article, Guid guid)
        {
            throw new Exception("The method or operation is not implemented.");
        }

        public void SaveComment(ArticleComment comment, string userId)
        {
            throw new Exception("The method or operation is not implemented.");
        }

        public void AddTag(int articleId, int tagId, Guid guid)
        {
            throw new Exception("The method or operation is not implemented.");
        }

        public void Remove(int articleId)
        {
            throw new Exception("The method or operation is not implemented.");
        }

        public void RemoveComment(int commentId)
        {
            throw new Exception("The method or operation is not implemented.");
        }

        public void RemoveTag(int articleTagId)
        {
            throw new Exception("The method or operation is not implemented.");
        }

        public void RemoveAllTags(int articleId)
        {
            throw new Exception("The method or operation is not implemented.");
        }

        public ArticleCollection FetchAll( bool onlyEnabled )
        {
            throw new Exception( "The method or operation is not implemented." );
        }

        #endregion
    }
}
